package org.hairball.analysis;

import java.util.ArrayList;
import java.util.List;

public class DescriptorReader {
    private ClassnameFilter classDescriptorFilter;

    public DescriptorReader() {
        this(new ASMClassDescriptorFilter());
    }

    public DescriptorReader(ClassnameFilter classnameFilter) {
        this.classDescriptorFilter = classnameFilter;
    }

    public List<String> getParameterTypes(String desc) {
        String snipped = removeReturnType(desc);
        snipped = removeLeadingBracket(snipped);
        List<String> types = new ArrayList<String>();

        String[] strings = snipped.split(";");

        for (String string : strings) {
            if (!string.equals("")) {
                types.add(classDescriptorFilter.filter(string));
            }
        }

        return types;
    }

    private String removeLeadingBracket(String snipped) {
        return snipped.substring(1, snipped.length());
    }

    private String removeReturnType(String desc) {
        int indexOfReturn = desc.indexOf(")");
        return desc.substring(0, indexOfReturn);
    }
}
